package com.aop.service.impl;

import com.aop.annotation.AddAnnotation;
import com.aop.annotation.ExtTransaction;
import com.aop.bean.User;
import com.aop.config.TransactionUtils;
import com.aop.dao.StudentDao;
import com.aop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author xinwang
 * @date 2020/2/22 18:13
 **/
@Service("userService")
public class UserServiceImpl implements UserService {


    @Autowired
    private StudentDao studentDao;

    @Autowired
    private TransactionUtils transactionUtils;

    @Override
    @ExtTransaction
    public void add() {
        System.out.println("向数据库添加数据！！");
        User user = new User();
        user.setName("李四");
        user.setSex(1);
        user.setAge(10);
        studentDao.add(user);
        int i= 1/0;
        User user2 = new User();
        user2.setName("王五");
        user2.setSex(2);
        user2.setAge(18);
        studentDao.add(user2);
    }


   /* @Override
    @Transactional
    public void add() {
        *//*TransactionStatus transactionStatus =null;
        try {
            transactionStatus=transactionUtils.begin();
            System.out.println("向数据库添加数据！！");
            User user = new User();
            user.setName("李四");
            user.setSex(1);
            user.setAge(10);
            studentDao.add(user);
            int i= 1/0;
            User user2 = new User();
            user2.setName("王五");
            user2.setSex(2);
            user2.setAge(18);
            studentDao.add(user2);
            transactionUtils.commit(transactionStatus);
        }catch (Exception e){

        }finally {
            if (transactionStatus!=null){
                transactionStatus.releaseSavepoint(transactionStatus);
            }
        }*//*

        *//*
          在使用spring事务的时候不要try 捕获异常，应该将异常抛出给外层 aop 异常通知进行回滚，
          如果要捕获哪就手动进行回滚   TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
         *//*
        System.out.println("向数据库添加数据！！");
        User user = new User();
        user.setName("李四");
        user.setSex(1);
        user.setAge(10);
        studentDao.add(user);
      *//*  int i= 1/0;*//*
        User user2 = new User();
        user2.setName("王五");
        user2.setSex(2);
        user2.setAge(18);
        studentDao.add(user2);
    }*/



}
